package md.NJavaBase;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import java.util.Timer;
import java.util.TimerTask;


public class BannerLoader implements ILoader {
    final String Tag = "BannerLoader";

    public BannerLoader(IBanner ad )
    {
        m_ad = ad;
    }

    public  int getPriority(){return m_ad.getPriority();}

    public IBanner getAD() {  return m_ad;  }

    public AdState getState(){return m_State;}

    public boolean isInvalidated()
    {
        if(m_State!=AdState.Ready) return  false;

        boolean re = m_ad.isInvalidated();
        if(re) m_State = AdState.None;
        return re;
    }

    //使该loader变成无效状态
    public void SetInvalidated() {
        m_State = AdState.None;
    }

    public void reLoad(IAdLoaderCallback callback)
    {
        if(m_State!=AdState.None) {
            Log.d(Tag,"skip reload video!");
            return;
        }

        m_State = AdState.Loading;

        m_callback = callback;

        if(m_ad.isInitialized())//广告已经初始化完成
        {
            doLoad();//立即开始装载
        }else
        {
            //等待广告初始化完成
            Timer timer = new Timer();// 实例化Timer类
            class  task extends TimerTask
            {
                public task(Timer timer )
                {
                    m_timer = timer;
                }

                public void run() {
                    class reloadRunnable implements Runnable
                    {
                        @Override public void run() {
                            //已经初始化完毕
                            if(m_ad.isInitialized()) {
                                m_timer.cancel();
                                doLoad();//装载
                            }
                        }
                    }
                    NJavaBase.getActivity().runOnUiThread(new  reloadRunnable( ));
                }

                Timer m_timer;
            }
            timer.schedule(new task( timer) , 5000,5000);// 毫秒
        }
    }

    //开始装载的时间点
    public long getStartLoadTime() { return m_lastTime; }

    void doLoad()
    {
        long now = System.currentTimeMillis();
        m_lastTime = now;
        doLoad2();
    }

    void doLoad2()
    {

        class loadAdCallback implements  IAdCallback
        {

            //广告加载成功
            public void onLoad()
            {


                class callbackRunnable implements Runnable
                {
                    @Override public void run() {
                        Log.d(Tag,String.format("ad ready %s",m_ad.adName()) );
                        m_State = AdState.Ready;
                        m_callback.onLoad();
                    }
                }
                NJavaBase.getActivity().runOnUiThread(new  callbackRunnable( ));


            }

            //可获得奖励
            public void onReward(){}

            //广告被关闭
            public void onClose(){}

            //加载错误
            public void onError( final String errCode){


                class callbackRunnable implements Runnable
                {
                    @Override public void run() {
                        Log.d(Tag,String.format("ad load error %s",m_ad.adName()) );
                        m_State = AdState.None;
                        m_callback.onError(errCode);

                    }
                }
                NJavaBase.getActivity().runOnUiThread(new  callbackRunnable( ));

            }


        }

        Log.d(Tag,String.format("Start loader %s",getAD().adName()));
        m_ad.reloadAd(new loadAdCallback( ));
    }



    IBanner m_ad;
    AdState m_State = AdState.None ;//广告状态
    IAdLoaderCallback m_callback;//回调
    long m_lastTime = 0;
}
